/**
 * umiHooks Model 文件
 * @author SPY
 * @date 2020/04/11
 */
import { Effect, Reducer } from 'umi';
import { queryList } from '@/services/umiHooks';

export interface umiHooksModelState {
  cardList?: Array<string | number>;
}

export interface umiHooksModelType {
  namespace: string;
  state: umiHooksModelState;
  effects: {
    fetchList: Effect;
  };
  reducers: {
    save: Reducer<umiHooksModelState>;
  };
}

const umiHooksModel: umiHooksModelType = {
  namespace: 'umiHooks',
  state: {
    cardList: [],
  },
  effects: {
    *fetchList({ payload }, { put, call }) {
      const response = yield call(queryList, payload);
      yield put({
        type: 'save',
        payload: {
          cardList: response.results,
        },
      });
    },
  },
  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },
  },
};

export default umiHooksModel;
